local Msg = require("core.Msg")
local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local EquipLogic = require("bag.EquipLogic")
local BagLogic = require("bag.BagLogic")
local EquipExcel = require("excel.Equip")
local tipinConfig = EquipExcel.equipQulity
local Grid = require("bag.Grid")
local RefineDefine = require("refine.RefineDefine")
local ShopLogic = require("shop.ShopLogic")
local RefineHandle = require("refine.HandlerL")
local HtmlUtil = require("common.HtmlUtil")
local ItemDefine = require("bag.ItemDefine")
local Log = require("common.Log")
local TaskCallbacks = require("task.TaskCallbacks")
local TaskDefine = require("task.Define")
local YYDaBiao = require("activity.operate.DaBiao")
local AcvityDefine = require("activity.Define")
local OfficialLogic = require("official.OfficialLogic")

function open(human)
  local sendMsg = Msg.gc.GC_EQUIP_OPEN
  sendMsg.type = 3
  sendMsg.qhInfo[0] = 0
  sendMsg.status[0] = human.db.equipLen
  for i=0, human.db.equipLen - 1 do
    sendMsg.status[i + 1] = 1    
    if human.db.equip[i].id ~= 0 then
      local equip = human.db.equip[i].data.equip
      local pos = EquipExcel.equip[human.db.equip[i].id].pos
      if equip.quality < 16 then
        local isEnough = true
        for j=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
          if BagLogic.getItemCount(human, tipinConfig[equip.quality + 1]["needItem"..pos][j][1]) < tipinConfig[equip.quality + 1]["needItem"..pos][j][2] then
            isEnough = false
          end
        end
        if isEnough and ObjHuman.getYinliang(human) >= tipinConfig[equip.quality + 1].needYinliang then
          sendMsg.status[i + 1] = 0
        end
      end
    end    
  end
  Msg.send(sendMsg, human.fd)
end

function chooseEquip(human, index, noSend)
  local sendMsg = Msg.gc.GC_CHOOSE_EQUIP
  local grid = human.db.equip[index]
  local pos = EquipExcel.equip[grid.id].pos
  local equip = grid.data.equip
  sendMsg.type = 3
  sendMsg.jipin[0] = 0
  sendMsg.nextEquip[0] = 0
  if equip.quality < 16 then    
    sendMsg.showRand = tipinConfig[equip.quality + 1].showRand / 100
    local cache = {}
    sendMsg.needItem[0] = #tipinConfig[equip.quality + 1]["needItem"..pos]
    for i=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
      Grid.create(cache, tipinConfig[equip.quality + 1]["needItem"..pos][i][1], tipinConfig[equip.quality + 1]["needItem"..pos][i][2])
      Grid.makeItem(cache, sendMsg.needItem[i], 0, human)
    end
    sendMsg.needYinliang = tipinConfig[equip.quality + 1].needYinliang
    sendMsg.nextAddAttr[0] = #EquipExcel.equip[grid.id].baseAttr 
    for i=1, #EquipExcel.equip[human.db.equip[index].id].baseAttr do
      sendMsg.nextAddAttr[i].key = EquipExcel.equip[human.db.equip[index].id].baseAttr[i][1]
      sendMsg.nextAddAttr[i].value = EquipExcel.equip[human.db.equip[index].id].baseAttr[i][2] * (tipinConfig[equip.quality + 1].attrAddRate - tipinConfig[equip.quality].attrAddRate)
    end
    sendMsg.nextEquip[0] = 1
    Grid.create(cache, grid.id, 1, false, equip.quality + 1, nil, equip.strengthLv)
    Grid.makeItem(cache, sendMsg.nextEquip[1], 0, human)
    sendMsg.isMax = 0
    sendMsg.stageNextLv = tipinConfig[equip.quality + 1].needLv
  else
    sendMsg.showRand = 0
    sendMsg.isMax = 2
    sendMsg.nextAddAttr[0] = 0
    sendMsg.needYinliang = 0
    sendMsg.needItem[0] = 0
    sendMsg.stageNextLv = 0
  end
  if noSend then
    return sendMsg.isMax ~= 2
  else
    Msg.send(sendMsg, human.fd)
  end
end

function tipin(human, index, isAutoBuy)
  local grid = human.db.equip[index]
  local equip = human.db.equip[index].data.equip
  local pos = EquipExcel.equip[grid.id].pos
  local needBuy = {}
  if equip.quality >= 16 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_TIPIN_MAX_QUALITY)
  end
  if tipinConfig[equip.quality + 1].needLv > human.db.lv then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_TIPIN_LV_NOT_ENOUGH)
  end
  local buyIndex = 0
  for i=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
    if BagLogic.getItemCount(human, tipinConfig[equip.quality + 1]["needItem"..pos][i][1]) < tipinConfig[equip.quality + 1]["needItem"..pos][i][2] then
      buyIndex = buyIndex + 1
      needBuy[buyIndex] = {}
      needBuy[buyIndex][1] = tipinConfig[equip.quality + 1]["needItem"..pos][i][1]
      needBuy[buyIndex][2] = tipinConfig[equip.quality + 1]["needItem"..pos][i][2] - BagLogic.getItemCount(human, tipinConfig[equip.quality + 1]["needItem"..pos][i][1])
    end  
  end
  
  if ObjHuman.getYinliang(human) < tipinConfig[equip.quality + 1].needYinliang then
    RefineHandle.CG_OPEN_REFINE_WINDOW(human)
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_YINLIANG_NOT_ENOUGH)
  end
  if #needBuy > 0 and isAutoBuy ~= 1 then
    RefineHandle.CG_OPEN_REFINE_WINDOW(human)
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_TIPIN_ITEM_NOT_ENOUGH)
  elseif #needBuy > 0 and isAutoBuy == 1 then
    if not ShopLogic.batchBuy(human, needBuy) then
      RefineHandle.CG_OPEN_REFINE_WINDOW(human)
      return
    end
  end
  for i=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
    if BagLogic.getItemCount(human, tipinConfig[equip.quality + 1]["needItem"..pos][i][1]) < tipinConfig[equip.quality + 1]["needItem"..pos][i][2] then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_TIPIN_ITEM_NOT_ENOUGH)
    end
  end
  for i=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
    BagLogic.delItemsById(human, tipinConfig[equip.quality + 1]["needItem"..pos][i][1], tipinConfig[equip.quality + 1]["needItem"..pos][i][2], "tipin")
  end
  ObjHuman.decYinliang(human, tipinConfig[equip.quality + 1].needYinliang, "tipin")
  
  local getRand = math.random(1, 10000)
  if getRand > tipinConfig[equip.quality + 1].successRand then
    chooseEquip(human, index)
    RefineDefine.sendStatus(human, RefineDefine.REFINE_TIPIN_FAIL)
    RefineHandle.CG_OPEN_REFINE_WINDOW(human)
    return
  end
  equip.quality = equip.quality + 1
  equip.hole = ItemDefine.getRealQuality(equip.quality) - 1
  EquipLogic.GC_EQUIP_CHANGE(human,index)
  open(human)
  chooseEquip(human, index)
  RefineDefine.sendStatus(human, RefineDefine.REFINE_TIPIN_SUCCESS)
  ObjHuman.doCalc(human)
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
  if equip.quality > 2 then
    local bro = string.format(Lang.REFINE_TIPIN_BROCAST, HtmlUtil.fontName(human.name, human._id), 
      HtmlUtil.fontItem(human.db.equip[index]), ItemDefine.getText(equip.quality))
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, bro)
  end
  OfficialLogic.checkOneCondition(human, "equipLv")
  YYDaBiao.checkCanGet(human, AcvityDefine.OPERATEACT_TYPE_EQUIPQUALITY)
  TaskCallbacks.taskRefineCB(human, TaskDefine.TASK_REFINE_TP, EquipExcel.equip[grid.id].pos)
  
  Log.write(Log.LOGID_OSS_REFINE_TP, human._id,human.account,human.name,human.db.lv,grid.id,EquipExcel.equip[grid.id].pos,equip.quality - 1,equip.quality)
end

function checkShowTips(human, info)
  if human.db.lv < 26 then
    return 0
  end
  local cache = {}
  for i=0,human.db.equipLen - 1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~= 0 then
      local equip = grid.data.equip
      local pos = EquipExcel.equip[grid.id].pos
      if chooseEquip(human, i, true) then
        local isTrue = true
        for i=1, #tipinConfig[equip.quality + 1]["needItem"..pos] do
          if BagLogic.getItemCount(human, tipinConfig[equip.quality + 1]["needItem"..pos][i][1]) < tipinConfig[equip.quality + 1]["needItem"..pos][i][2] 
            or ObjHuman.getYinliang(human) < tipinConfig[equip.quality + 1].needYinliang then
            isTrue = false
            break
          end
        end
        if isTrue then
          if info then
            info.equip[0] = 1
            Grid.create(cache, grid.id, 1)
            Grid.makeItem(cache, info.equip[1], 0, human)
          end  
          return 1
        end
      end
    end
  end
  return 0
end
